/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.server.file;

import swym.server.*;

import java.util.*;

/**
 *
 * @author mschaefer
 */
public class FileAction
{
    private SyncedFile file;
    private HashMap<Client, Boolean> clientTable;
    
    public enum eFileActionType { NEW, MOD, DELETE, CHUNK, MARK, GET, STATUS }
    
    private eFileActionType type;
    
    private final boolean DEBUG = true;
    
    public FileAction( SyncedFile file, eFileActionType type )
    {
        this.file = file;
        
        this.type = type;
        
        clientTable = new HashMap<Client, Boolean>();
        
        for ( Client client : file.getOwner().getClients() )
            clientTable.put( client, false );
    }
    
    /**
     * Adds a client to the client table, with a 
     * non-update marking.
     * 
     * @param client
     */
    public void addClient( Client client )
    {
        synchronized ( clientTable )
        {
            clientTable.put( client, false );
        }
    }
    
    public Vector<Client> getClients()
    {
        synchronized ( clientTable )
        {
            return new Vector<Client>( clientTable.keySet() );
        }
    }
    
    public SyncedFile getFile()
    {
        return file;
    }
    
    public eFileActionType getType()
    {
        return type;
    }
    
    public void setClientUpdated( Client client )
    {
        synchronized ( clientTable )
        {
            if ( clientTable.keySet().contains( client ) )
            {
                clientTable.put( client, true );
                
                if ( DEBUG )
                    System.out.println( client.getName() + " updated file action: " + 
                                        this.toString() );
            }
        }
    }
    
    /**
     * Return a hash map of the client names and a 
     * boolean reflecting whether they have updated
     * from this action.
     * @return
     */
    public HashMap<String, Boolean> getClientTable()
    {
        HashMap<String, Boolean> returnTable = new HashMap<String, Boolean>();
        
        synchronized ( clientTable )
        {
            Iterator<Client> iter = clientTable.keySet().iterator();
            
            while ( iter.hasNext() )
            {
                Client client = iter.next();
                returnTable.put( client.getName(), clientTable.get( client ) );
            }
        }
        
        return returnTable;
    }
    
    public boolean isClientUpdated( Client client )
    {
        Boolean updated = null;
        
        synchronized ( clientTable )
        {
            updated = clientTable.get( client );
        }
        
        return ( updated != null ) && updated.booleanValue();
    }
    
    public boolean allClientsUpdated()
    {
        synchronized ( clientTable )
        {
            for ( Client client : clientTable.keySet() )
                if ( !clientTable.get( client ) )
                    return false;
        }

        return true;
    }
    
    public String toString()
    {
        return type.toString() + " file action: " + file.toString();
    }
}
